package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.exception.*;

/**
 * Class for saving accounts without overdraft
 * @author alex
 */
public class SavingAccount extends AbstractAccount {	
	/**
	 * @param balance the balance
	 */
	public SavingAccount(double balance) {
		super(balance);
	}
	
	/**
	 * @param x the money
	 * @throws NotEnoughFundsException 
	 */
	public void withdraw(double x) throws NotEnoughFundsException {
		if (this.balance >= x) {
			this.balance -= x;
		} else {
			throw new NotEnoughFundsException();
		}
	}
	
	/**
	 * @return the maximum amount to withdraw
	 */
	public double maximumAmountToWithdraw() {
		return this.balance;
	}
	
	/**
	 * @return the sting presentation
	 */
	@Override
	public String toString() {
		return "Saving account, balance: " + this.balance;
	}
	
	/**
	 * @return the saving account object hash code
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((client == null) ? 0 : client.hashCode());
		return result;
	}
	
	/**
	 * @param obj the saving account
	 * @return whether saving accounts are equal
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SavingAccount other = (SavingAccount) obj;
		if (Double.doubleToLongBits(balance) != Double.doubleToLongBits(other.balance))
			return false;
		if (client == null) {
			if (other.client != null)
				return false;
		} else if (!client.equals(other.client))
			return false;
		return true;
	}
}
